

import 'package:flutter/material.dart';

import 'package:moonplay/common/app_common.dart';
import 'package:moonplay/common/event_bus_utils.dart';
import 'package:moonplay/pages/common_page/common_filter_page.dart';
import 'package:moonplay/pages/views/index/one_index_page.dart';
import 'package:moonplay/pages/views/index/recommend_page.dart';
import 'package:get/get.dart';
import 'package:moonplay/pages/views/index/video_list_page.dart';
import 'package:moonplay/widgets/popup_window.dart';

class OnePage extends StatefulWidget {
  @override
  _OnePageState createState() => _OnePageState();
}

class _OnePageState extends State<OnePage> with SingleTickerProviderStateMixin {
  List<String> _tabs = ['推荐', '电影', '电视剧', '综艺', '动漫'];
  TabController _tabController;
  int tabIndex = 0;
  int typeIndex = 0;
  int yearIndex = 0;
  
  GlobalKey _positionKey = GlobalKey();

  @override
  void initState() {
    super.initState();

    _tabController = TabController(vsync: this, initialIndex: 0, length: 5)
    ..addListener(() {
      setState(() {
        tabIndex = _tabController.index;
        typeIndex = 0;
        yearIndex = 0;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: _tabs.length,
      child: Scaffold(
        appBar: CustomAppbar(
          title: _widgetTitle(),
          navigationBarBackgroundColor: Theme.of(context).primaryColorLight,
          leadingWidget: leftBarButtonItemsWidget(),
          trailingWidget: rightBarButtonItemsWidget(),
        ),
        body: TabBarView(
          controller: _tabController,
          children: [
            RecommendPage(),
            VideoListPage(type: 1),
            VideoListPage(type: 2),
            VideoListPage(type: 3),
            VideoListPage(type: 4),
          ],
        ),
      )
    );
  }

  Widget leftBarButtonItemsWidget(){
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: (){
        Get.toNamed("/SearchPage");
      },
      child: Container(
        width: ScreenUtil().getWidthPx(100),
        height: ScreenUtil().getWidthPx(100),
        alignment: Alignment.center,
        color: Colors.transparent,
        child: Icon(IconData(0xe752, fontFamily: Constants.IconFontFamily),color: Colors.white, size: 19),
      ),
    );
  }

  Widget rightBarButtonItemsWidget(){
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: (){
        _showFilter(context);
      },
      child: Container(
        key: _positionKey,
        width: ScreenUtil().getWidthPx(100),
        height: ScreenUtil().getWidthPx(100),
        alignment: Alignment.center,
        color: Colors.transparent,
        child: Icon(IconData(0xe650, fontFamily: Constants.IconFontFamily),color: Colors.white, size: 22),
      ),
    );
  }

  Widget _widgetTitle(){
    return Container(
      padding: EdgeInsets.only(right: 50, left: 50),
      child: TabBar(
        isScrollable: true,
        controller: _tabController,
        indicatorWeight: 2.0,
        indicatorPadding: EdgeInsets.all(4),
        labelPadding: EdgeInsets.only(left: 16.0, right: 16.0),
        indicator: CommonUnderlineTabIndicator(borderSide:  BorderSide(width: 3.0), lineColor: Colors.blue),
        indicatorSize: TabBarIndicatorSize.label,
        indicatorColor: Color(0xff4fe6c0),
        tabs: _tabs.map((val) =>
          new Tab(
            text: val,
          )).toList(),
      ),
    );
  }

  /**
   * 弹出筛选弹窗
   * _backData为关闭弹窗时的回调函数
   */
  _showFilter(context){
    final RenderBox button = _positionKey.currentContext.findRenderObject();
    final RenderBox overlay = Overlay.of(context).context.findRenderObject();
    var a =  button.localToGlobal(Offset(button.size.width + 12.0, button.size.height), ancestor: overlay);
    var b =  button.localToGlobal(button.size.bottomLeft(Offset(0, 12)), ancestor: overlay);
    final RelativeRect position = RelativeRect.fromRect(
      Rect.fromPoints(a, b),
      Offset.zero & overlay.size,
    );
    double _screenHeight = MediaQuery.of(context).size.height;
    double _viewHeight = _screenHeight - a.dy;
    showPopupWindow(
      context: context,
      fullWidth: true,
      isShowBg: false,   //是否显示背景遮罩
      position: position,
      elevation: 0.0,
      child: CommonFilterPage(height: _viewHeight, backFun: _backData, type: tabIndex, typeIndex: typeIndex, yearIndex: yearIndex),
    );
  }

  _backData(typeId, vodYear, _typeIndex, _yearIndex) async {
    logger.d('回调参数$typeId, $vodYear, $_typeIndex, $_yearIndex');
    if(vodYear == "全部") {
      vodYear = "";
    }
    if(vodYear == "其它") {
      vodYear = "2010";
    }
    
    eventBus.fire(new RefVideoList(tabIndex, typeId, vodYear)); 
    await Future.delayed(Duration(milliseconds: 500));
    setState(() {
      typeIndex = _typeIndex;
      yearIndex = _yearIndex;
    });
  }

  
}
